package com.atgugui.eduservice.controller;


import com.atgugui.commonutils.Result;
import com.atgugui.eduservice.entity.EduCourse;
import com.atgugui.eduservice.entity.vo.CourseInfoVo;
import com.atgugui.eduservice.entity.vo.CoursePublishVo;
import com.atgugui.eduservice.entity.vo.VoCourseQuery;
import com.atgugui.eduservice.service.EduChapterService;
import com.atgugui.eduservice.service.EduCourseDescriptionService;
import com.atgugui.eduservice.service.EduCourseService;
import com.atgugui.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-29
 */
@Api(tags = "课程信息")
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @ApiOperation(value = "添加课程信息(基本信息和描述信息)")
    @PostMapping("/addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.addCourseInfo(courseInfoVo);
        return Result.ok().data("courseId",id);
    }

    @ApiOperation(value = "根据id查询出课程信息")
    @GetMapping("/getCourseInfoById/{courseId}")
    public Result getCourseInfoById(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfoById(courseId);
        return Result.ok().data("courseInfoVo",courseInfoVo);
    }

    @ApiOperation(value = "更新课程信息")
    @PostMapping("/updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    //根据课程id查询课程确认信息
    @ApiOperation(value = "确认发布课程总体信息")
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public Result getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(courseId);
        return Result.ok().data("publishCourse",coursePublishVo);
    }

    @ApiOperation(value = "最终发布")
    @GetMapping("/publishCourse/{courseId}")
    public Result publishCourse(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");//设置课程发布状态
        eduCourseService.updateById(eduCourse);
        return Result.ok();
    }

    @ApiOperation(value = "无条件查询")
    @GetMapping("/getCourseList")
    public Result getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return Result.ok().data("courseList",list);
    }

    @ApiOperation(value = "不带条件的分页查询")
    @GetMapping("/pageCourse/{current}/{limit}")
    public Result pageListCourse(@PathVariable Long current, @PathVariable Long limit) {
        Page<EduCourse> page = new Page<>(current,limit); // 创建分页对象
        eduCourseService.page(page,null); // 分页查询
        long total = page.getTotal();
        List<EduCourse> items = page.getRecords();
        return Result.ok().data("total",total).data("items",items);
    }

    @ApiOperation(value = "条件分页查询")
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public Result pageListCourseByCondition(@PathVariable Long current,
                                            @PathVariable Long limit,
                                            @RequestBody VoCourseQuery voCourseQuery) {
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>(); // 创建条件查询对象
        String status = voCourseQuery.getStatus();
        String title = voCourseQuery.getTitle();
        if (!StringUtils.isEmpty(title)) {
            courseQueryWrapper.eq("title",title);
        }
        if (!StringUtils.isEmpty(status)) {
            courseQueryWrapper.eq("status",status);
        }
        courseQueryWrapper.orderByDesc("gmt_create"); // 按照创建课程的时间排序
        Page<EduCourse> coursePage = new Page<>(current,limit); // 创建分页对象
        eduCourseService.page(coursePage,courseQueryWrapper); // 带有条件的分页查询
        long total = coursePage.getTotal();
        List<EduCourse> items = coursePage.getRecords();
        return Result.ok().data("total",total).data("items",items);
    }

    @ApiOperation(value = "按照课程id联级删除课程相关信息")
    @GetMapping("/deleteCourseById/{courseId}")
    public Result deleteCourseById(@PathVariable String courseId) {
        eduCourseService.removeCourseById(courseId);
        return Result.ok();
    }
}

